package com.zzx.threekingdom.view;


import com.zzx.threekingdom.judge.Judge;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.view.PlayerView.PressListener;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;

public class OtherPlayersView extends ViewGroup implements PressListener
{
    public interface SelectListener
    {
        void onSelected(ArrayList<PlayerView> pvs);
    }
    
    private int selectMaxNum = 0;
    private ArrayList<PlayerView> selectedPvs = new ArrayList<PlayerView>();
    private Rect mTouchFrame = new Rect();
    private SelectListener selectListener = null;
    
    public OtherPlayersView(Context context)
    {
        this(context, null);
    }
    public OtherPlayersView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    public OtherPlayersView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        selectedPvs.clear();
    }
    
    public ArrayList<PlayerView> getSelectedPm() { return selectedPvs; }
    public void setSelcetListener(SelectListener listener) { selectListener = listener; }
    public void notifySelcetListener() { if(selectListener != null) selectListener.onSelected(getSelectedPm()); }
    
    public void addPlayer(PlayerView playerView)
    {
        addView(playerView);
    }
    public PlayerView addNewPlayerView()
    {
        PlayerView p = new PlayerView(getContext()); 
        addView(p);
        return p;
    }
    public void clearView()
    {
        for(int i = 0;i < getChildCount();i++)
            removeViewAt(i--);
        selectMaxNum = 0;
        selectedPvs.clear();
    }
    
    @Override
    public void addView(View child, int index, LayoutParams params)
    {
        if(child instanceof PlayerView)
        {
            super.addView(child, index, params);
            ((PlayerView)child).setListener(this);
        }
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int width = getPaddingRight() + getPaddingLeft();
        int height = getPaddingBottom() + getPaddingTop();
        final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        for(int i = 0;i < getChildCount();i++)
        {
            getChildAt(i).measure(freeSpec, freeSpec);
            width += getChildAt(i).getMeasuredWidth();
            height = Math.max(height, getChildAt(i).getMeasuredHeight());
        }
        
        setMeasuredDimension(width, height);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        int right = getMeasuredWidth();
        
        for(int i = 0;i < getChildCount();i++)
        {
            View child = getChildAt(i);
            child.layout(right - child.getMeasuredWidth(), 0, right, child.getMeasuredHeight());
            right -= child.getMeasuredWidth();
        }
    }
    
    public void onPressed(PlayerView playerView)
    {
        if((selectMaxNum==0) || (selectedPvs.contains(playerView)==true))
            return;
        selectedPvs.add(playerView);
        while(selectedPvs.size()>selectMaxNum)
        {
            selectedPvs.get(0).setSelected(false);
            selectedPvs.remove(0);
        }
        notifySelcetListener();
    }
    
    public void setNoSelectMode()
    {
        selectMaxNum = 0;
        for(int i = 0;i < getChildCount();i++)
        {
            View child = getChildAt(i);
            if(child instanceof PlayerView)
            {
                ((PlayerView)child).setSelected(false);
                ((PlayerView)child).setCanSelected(true);
            }
        }
        selectedPvs.clear();
    }
    
    public void setSelectedMode(int maxNum, ArrayList<AbsPlayerModel> pms)
    {
        if(maxNum<=0)
            return;
        selectMaxNum = maxNum;
        for(int i = 0;i < getChildCount();i++)
        {
            View child = getChildAt(i);
            if(child instanceof PlayerView)
            {
                PlayerView pv = (PlayerView)child;
                AbsPlayerModel pm = Judge.judge.getModelByPlayerView(pv);
                if((pms!=null)&&(pms.contains(pm)==true))
                    pv.setCanSelected(true);
                else
                    pv.setCanSelected(false);
            }
        }
        selectedPvs.clear();
    }
    
    @Override
    public boolean onTouchEvent (MotionEvent event)
    {
        if(selectMaxNum==0)
            return super.onTouchEvent(event);
        
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                PlayerView pv = pointToView((int)event.getX(), (int)event.getY());
                if((pv != null)&&(pv.isSelected()==false))
                    pv.setSelected(true);
                break;
            default:
                break;
        }

        return true;
    }
    
    public PlayerView pointToView(int x, int y)
    {
        final int count = getChildCount();
        for (int i = count - 1; i >= 0; i--)
        {
            View child = getChildAt(i);
            if((child instanceof PlayerView)&&(child.isEnabled()==true))
            {
                child.getHitRect(mTouchFrame);
                if(mTouchFrame.contains(x, y))
                    return (PlayerView)child;
            }
        } 
        return null;
    }
    
    

}
